package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author SunSun
 * @date 2021/10/23
 */
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     *获得用户详细信息
     * @param token
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token,Long userID){
        if(userID==null){
            userID=UserHolder.getUserId();
        }
        UserInfoVo userInfo=userInfoService.findByid(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 修改详细信息
     * @param token
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestHeader("Authorization") String token,
                                      @RequestBody UserInfo userInfo){

        userInfo.setId(UserHolder.getUserId());

        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更换头像
     * @param token
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(@RequestHeader("Authorization") String token,MultipartFile headPhoto) throws IOException {

        userInfoService.updateHead(headPhoto,UserHolder.getUserId());

        return ResponseEntity.ok(null);

    }

    /**
     * 查询喜欢,粉丝 互相喜欢数量
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map map=userInfoService.counts();
        return ResponseEntity.ok(map);
    }

}
